<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        //1.商品数据结构, 按类型分组
        var data = [
            {
                type: 'jiadian',
                id: 'A001',
                smallImg: '商品小图片',
                title: '最新家电',
                largeImg: '商品大图片',
                price: 178,
                isPlus: true,
                plusPrice: 120,
                isDiscount: false,
                isJD: true,
                name: '电饭锅'
            },
            {
                type: 'shengxian',
                id: 'B912',
                smallImg: '商品小图片',
                title: '新鲜花生',
                largeImg: '商品大图片',
                price: 28,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: false,
                name: '广西花生'
            },
            {
                type: 'shengxian',
                id: 'B610',
                smallImg: '商品小图片',
                title: '大颗葵米',
                largeImg: '商品大图片',
                price: 4,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: '葵米'
            },
            {
                type: 'dianzi',
                id: 'CF01',
                smallImg: '商品小图片',
                title: '最新款华为5G手机',
                largeImg: '商品大图片',
                price: 18888,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: '华为5G手机'
            },
            {
                type: 'jiadian',
                id: 'PU09',
                smallImg: '商品小图片',
                title: '高性能电磁炉',
                largeImg: '商品大图片',
                price: 456,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: '电磁炉'
            },
            {
                type: 'fuzhuang',
                id: 'OM10',
                smallImg: '商品小图片',
                title: '上新T恤',
                largeImg: '商品大图片',
                price: 139,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: 'T恤'
            },
            {
                type: 'jiadian',
                id: 'KKM01',
                smallImg: '商品小图片',
                title: '热水壶',
                largeImg: '商品大图片',
                price: 68,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: '静音热水壶'
            },
            {
                type: 'shengxian',
                id: 'UUY01',
                smallImg: '商品小图片',
                title: '鲜虾',
                largeImg: '商品大图片',
                price: 398,
                isPlus: false,
                plusPrice: 0,
                isDiscount: true,
                isJD: true,
                name: '九节虾'
            }
        ];
        function callClassify(data) {
            var all = {};
            for (let i = 0; i < data.length; i++) {
                if (!all[data[i].type]) all[data[i].type] = [];
                for (var key in all) {
                    if (data[i].type == key) all[key].push(data[i])
                }
            }
            return all;
        };
        console.log(callClassify(data));

        //2.合并节点树(原理就是对象深拷贝)
        var tree1 = {
            val: 1,
            left: {
                val: 3,
                left: {
                    val: 5
                }
            },
            right: {
                val: 2
            }
        };
        var tree2 = {
            val: 2,
            left: {
                val: 1,
                right: {
                    val: 4
                }
            },
            right: {
                val: 3,
                right: {
                    val: 7,
                    left:{
                        val:18
                    }
                }
            }
        };
        //参数为两个节点树
        function callAdd(tree1, tree2) {
            if (!tree1 && !tree2) return null;
            return {
                val: ((tree1 ? tree1.val : 0) + (tree2 ? tree2.val : 0)),
                //两个节点数同步进行遍历，这样得到的val值都是在同一个方向，同一个阶级，函数返回的对象对应left节点
                left: callAdd((tree1&&tree1.left ? tree1.left : null), (tree2&&tree2.left ? tree2.left : null)),
                right: callAdd((tree1&&tree1.right ? tree1.right : null), (tree2&&tree2.right ? tree2.right : null))
            }
        }
        console.log(callAdd(tree1, tree2));
    </script>
</body>

</html>